package com.systop.ltbook.order.cart.webapp;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.systop.core.webapp.struts2.action.BaseAction;
import com.systop.ltbook.book.model.Book;
import com.systop.ltbook.book.service.BookManager;
import com.systop.ltbook.order.cart.model.Cart;
import com.systop.ltbook.order.cart.model.CartItem;

@SuppressWarnings("serial")
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CartAction extends BaseAction {
	@Autowired
	private BookManager bookManager;

	private Integer[] bookId;

	private Integer[] count;

	@SuppressWarnings("unchecked")
	public String books() {
		String hql = "from Book";
		List list = bookManager.query(hql);
		getRequest().setAttribute("list", list);
		return "books";
	}

	private CartItem item = new CartItem();
	/**
	 * json返回结果
	 */
	private Map<String, String> jsonResult;
	private String result;

	public String viewCart() {
		return "viewCart";
	}

	public String add() {
		// 根据得到的book的id查询得到Book实体
		Book book = bookManager.get(item.getBook().getId());
		Cart cart = getCartFromSession();
		item.setBook(book);
		cart.addItem(item);

		return "viewCart";
	}

	public String modifyCart() {
		Cart cart = getCartFromSession();
		for (int i = 0; i < bookId.length; i++) {
			Integer id = bookId[i];
			cart.modify(id, count[i]);
		}
		return "viewCart";
	}

	/**
	 * 修改购物车项中的数量
	 * 
	 * @return
	 */

	public String modifyCount() {
		// jsonResult = Collections.synchronizedMap(new
		// HashMap<String,String>());
		item.setBook(bookManager.get(item.getBook().getId()));
		Cart cart = getCartFromSession();
		cart.modifyCount(item);

		// jsonResult.put("result", "success");

		result = JSONObject.fromObject(cart).toString();
		logger.debug(result);

		return "json";

	}

	/**
	 * 删除购物车项
	 * 
	 * @return
	 */
	public String removeItem() {
		Cart cart = getCartFromSession();
		cart.removeItem(item.getBook().getId());

		return "viewCart";
	}

	/**
	 * 清空购物车
	 * 
	 * @return
	 */
	public String clear() {
		Cart cart = getCartFromSession();
		cart.clear();

		return "";
	}

	/**
	 * 从Session中删除购物车
	 * 
	 * @return
	 */
	public String removeCart() {
		HttpSession session = getRequest().getSession();
		session.removeAttribute("cart");
		return "";
	}

	private Cart getCartFromSession() {
		HttpSession session = getRequest().getSession(true);
		// 尝试从session中得到购物车
		Cart cart = (Cart) session.getAttribute("cart");
		// 如果购物车为null,则意味着session中还没有购物车
		if (cart == null) {
			// 创建购物车
			cart = new Cart();
			session.setAttribute("cart", cart);
		}

		return cart;
	}

	public CartItem getItem() {
		return item;
	}

	public void setItem(CartItem item) {
		this.item = item;
	}

	public Map<String, String> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, String> jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Integer[] getBookId() {
		return bookId;
	}

	public void setBookId(Integer[] bookId) {
		this.bookId = bookId;
	}

	public Integer[] getCount() {
		return count;
	}

	public void setCount(Integer[] count) {
		this.count = count;
	}

}
